package com.css.zfzx.fwsjcj.modules.report.controller;

import com.alibaba.fastjson.JSONObject;
import com.css.bpm.platform.base.response.RestResponse;
import com.css.bpm.platform.utils.PlatformDateUtils;
import com.css.zfzx.fwsjcj.commen.util.Constant;
import com.css.zfzx.fwsjcj.config.image.ConfigBeanValue;
import com.css.zfzx.fwsjcj.modules.analysis.vo.AnalysisQueryVo;
import com.css.zfzx.fwsjcj.modules.report.service.ExportReportService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import sun.misc.BASE64Decoder;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 生成普查报告
 */
@RestController
@RequestMapping("/fw/pcReport")
@Slf4j
public class PCReportController {
    @Autowired
    private ConfigBeanValue configBeanValue;
    @Autowired
    ExportReportService exportReportService;

    @GetMapping("exportReport")
    public RestResponse exportReport(String province, String city,String area,String taskName,String timeStamp,HttpServletRequest request, HttpServletResponse response) {
        RestResponse restResponse = null;
        JSONObject jsonObject = new JSONObject();
        try {

            Integer i = exportReportService.exportReport(province, city,area,taskName,timeStamp, request, response);
            if (i > 0 ){
                jsonObject.put("message", "生成报告成功");
                restResponse = RestResponse.succeed(jsonObject);
            }else {
                jsonObject.put("message", "生成报告失败，该区域没有任务！");
                restResponse = RestResponse.succeed(jsonObject);
            }
        } catch (Exception e) {
            String errormessage = "生成报告失败";
            log.error(errormessage, e);
            restResponse = RestResponse.fail(errormessage);
        }
        return restResponse;
    }

    /**
     * 将base64编码转成图片格式并保存到本地，并返回虚拟路径
     *
     * @param base64Info
     * @param fileName
     * @return
     */
    @RequestMapping("/image")
    public Map<String, String> image(String base64Info, String fileName,String timeStamp) {
        String targetDirectory = configBeanValue.wordImage;
        String virtualPath = configBeanValue.virtualPath;
        //确保图片名称的唯一性
        String filename = timeStamp + fileName + ".png";
        String imagePath = virtualPath + File.separator + filename;
        String savePath = targetDirectory + File.separator + filename;
        base64Info = base64Info.split("base64,")[1];
        try {
            BASE64Decoder decoder = new BASE64Decoder();
            byte[] data = decoder.decodeBuffer(base64Info);
            // 调整异常数据
            for (int i = 0; i < data.length; ++i) {
                if (data[i] < 0) {
                    data[i] += 256;
                }
            }
            File file = new File(targetDirectory);
            if (!file.exists()) {
                file.mkdirs();
            }
            OutputStream os = new FileOutputStream(savePath);
            os.write(data);
            os.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        Map<String, String> map = new HashMap<>();
        map.put("imageUrl", imagePath);
        return map;
    }

    /**
     * 统计分析
     *
     * @param analysisQueryVo
     * @return
     */
    @GetMapping("analysisExport")
    public RestResponse analysisExport(AnalysisQueryVo analysisQueryVo) {
        RestResponse restResponse = null;
        JSONObject jsonObject = new JSONObject();
        //查询审核通过的数据
        analysisQueryVo.setSpType(Constant.SP_YES);
        try {
            //柱状建筑数量
            Map<String, Object> buildingNumColumn = exportReportService.buildingNumData(AnalysisQueryVo.getOne(analysisQueryVo));
            //柱状建筑面积
            Map<String, Object> buildingAreaColum = exportReportService.buildingAreaData(AnalysisQueryVo.getOne(analysisQueryVo));
            //城市建筑结构类型数量
            List<Map> buildingCityNumDataByStructureType = exportReportService.buildingCityNumDataByStructureType(AnalysisQueryVo.getOne(analysisQueryVo));
            //城市建筑结构类型面积
            List<Map> buildingCityAreaDataByStructureType = exportReportService.buildingCityAreaDataByStructureType(AnalysisQueryVo.getOne(analysisQueryVo));
            //农村建筑结构类型数量
            List<Map> buildingVillageNumDataByStructureType = exportReportService.buildingVillageNumDataByStructureType(AnalysisQueryVo.getOne(analysisQueryVo));
            //农村建筑结构类型面积
            List<Map> buildingVillageAreaDataByStructureType = exportReportService.buildingVillageAreaDataByStructureType(AnalysisQueryVo.getOne(analysisQueryVo));
            //城镇住宅年代数量
            List<Map> buildingNumCityByBuildingTime = exportReportService.buildingNumCityByBuildingTime(AnalysisQueryVo.getOne(analysisQueryVo));
            //城镇柱宅年代面积
            List<Map> buildingAreaCityByBuildingTime = exportReportService.buildingAreaCityByBuildingTime(AnalysisQueryVo.getOne(analysisQueryVo));
            //城镇非住宅年代数量
            List<Map> buildingNumNoCityByBuildingTime = exportReportService.buildingNumNoCityByBuildingTime(AnalysisQueryVo.getOne(analysisQueryVo));
            //城镇非住宅年代面积
            List<Map> buildingAreaNocityeByBuildingTime = exportReportService.buildingNumNoCityByBuildingTime(AnalysisQueryVo.getOne(analysisQueryVo));
            //农村住宅年代数量
            List<Map> buildingNumVillageByBuildingTime = exportReportService.buildingNumVillageByBuildingTime(AnalysisQueryVo.getOne(analysisQueryVo));
            //农村住宅年代面积
            List<Map> buildingAreaVillageByBuildingTime = exportReportService.buildingAreaVillageByBuildingTime(AnalysisQueryVo.getOne(analysisQueryVo));
            //农村公共建筑年代数量
            List<Map> buildingNumVillagePubByBuildingTime = exportReportService.buildingNumVillagePubByBuildingTime(AnalysisQueryVo.getOne(analysisQueryVo));
            //农村公共建筑年代面积
            List<Map> buildingAreaVillagePubByBuildingTime = exportReportService.buildingAreaVillagePubByBuildingTime(AnalysisQueryVo.getOne(analysisQueryVo));
            //城镇住宅抗震烈防强度数量
            List<Map> buildingNumCityByFortificationIntensity = exportReportService.buildingNumCityByFortificationIntensity(AnalysisQueryVo.getOne(analysisQueryVo));
            //城镇住宅抗震烈防强度面积
            List<Map> buildingAreaCityByFortificationIntensity = exportReportService.buildingAreaCityByFortificationIntensity(AnalysisQueryVo.getOne(analysisQueryVo));
            //城镇非住宅抗震烈防强度数量
            List<Map> buildingNumNoCityByFortificationIntensity = exportReportService.buildingNumNoCityByFortificationIntensity(AnalysisQueryVo.getOne(analysisQueryVo));
            //城镇非住宅抗震烈防强度面积
            List<Map> buildingAreaNoCityByFortificationIntensity = exportReportService.buildingAreaNoCityByFortificationIntensity(AnalysisQueryVo.getOne(analysisQueryVo));
            //农村住宅抗震烈防强度数量
            List<Map> buildingNumVillageByFortificationIntensity = exportReportService.buildingNumVillageByFortificationIntensity(AnalysisQueryVo.getOne(analysisQueryVo));
            //农村住宅抗震烈防强度面积
            List<Map> buildingAreaVillageByFortificationIntensity = exportReportService.buildingAreaVillageByFortificationIntensity(AnalysisQueryVo.getOne(analysisQueryVo));
            //农村公共建筑抗震设防强度数量
            List<Map> buildingNumVillagePubByFortificationIntensity = exportReportService.buildingNumVillagePubByFortificationIntensity(AnalysisQueryVo.getOne(analysisQueryVo));
            //农村公共建筑抗震设防强度面积
            List<Map> buildingAreaVillagePubByFortificationIntensity = exportReportService.buildingAreaVillagePubByFortificationIntensity(AnalysisQueryVo.getOne(analysisQueryVo));
            //城市住宅抗震设防情况数量
            List<Map> buildingNumCityByResult = exportReportService.buildingNumCityByResult(AnalysisQueryVo.getOne(analysisQueryVo));
            //城市住宅抗震设防情况面积
            List<Map> buildingAreaCityByResult = exportReportService.buildingAreaCityByResult(AnalysisQueryVo.getOne(analysisQueryVo));
            //城市住宅安全隐患数量
            List<Map> buildingNumCityByBuildingIsDamage = exportReportService.buildingNumCityByBuildingIsDamage(AnalysisQueryVo.getOne(analysisQueryVo));
            //城镇住宅安全隐患面积
            List<Map> buildingAreaCityByBuildingIsDamage = exportReportService.buildingAreaCityByBuildingIsDamage(AnalysisQueryVo.getOne(analysisQueryVo));
            //城镇非住宅抗震设防统计数量
            List<Map> buildingNumNoCityByResult = exportReportService.buildingNumNoCityByResult(AnalysisQueryVo.getOne(analysisQueryVo));
            //城镇非住宅抗震设防面积
            List<Map> buildingAreaNoCityByResult = exportReportService.buildingAreaNoCityByResult(AnalysisQueryVo.getOne(analysisQueryVo));
            //城镇非住宅安全隐患数量
            List<Map> buildingNumNoCityByBuildingIsDamage = exportReportService.buildingNumNoCityByBuildingIsDamage(AnalysisQueryVo.getOne(analysisQueryVo));
            //城镇非住宅安全隐患面积
            List<Map> buildingAreaNoCityByBuildingIsDamage = exportReportService.buildingAreaNoCityByBuildingIsDamage(AnalysisQueryVo.getOne(analysisQueryVo));
            //农村住宅抗震设防数量
            List<Map> buildingNumVillageByResult = exportReportService.buildingNumVillageByResult(AnalysisQueryVo.getOne(analysisQueryVo));
            //农村住宅抗震设防面积
            List<Map> buildingAreaVillageByResult = exportReportService.buildingAreaVillageByResult(AnalysisQueryVo.getOne(analysisQueryVo));
            //农村住宅安全隐患数量
            List<Map> buildingNumVillageByBuildingIsDamage = exportReportService.buildingNumVillageByBuildingIsDamage(AnalysisQueryVo.getOne(analysisQueryVo));
            //农村住宅安全隐患面积
            List<Map> buildingAreaVillageByBuildingIsDamage = exportReportService.buildingAreaVillageByBuildingIsDamage(AnalysisQueryVo.getOne(analysisQueryVo));
            //农村住宅抗震构造措施数量
            List<Map> buildingNumVillageByHouseMeasures = exportReportService.buildingNumVillageByHouseMeasures(AnalysisQueryVo.getOne(analysisQueryVo));
            //农村住宅抗震构造措施面积
            List<Map> buildingAreaVillageByHouseMeasures = exportReportService.buildingAreaVillageByHouseMeasures(AnalysisQueryVo.getOne(analysisQueryVo));
            //农村住宅曾受灾情况数量
            List<Map> buildingNumVillageByOnceAffected = exportReportService.buildingNumVillageByOnceAffected(AnalysisQueryVo.getOne(analysisQueryVo));
            //农村住宅曾受灾情况面积
            List<Map> buildingAreaVillageByOnceAffected = exportReportService.buildingAreaVillageByOnceAffected(AnalysisQueryVo.getOne(analysisQueryVo));
            //农村公共建筑抗震设防数量
            List<Map> buildingNumVillagePubByResult = exportReportService.buildingNumVillagePubByResult(AnalysisQueryVo.getOne(analysisQueryVo));
            //农村公共建筑抗震设防面积
            List<Map> buildingAreaVillagePubByResult = exportReportService.buildingAreaVillagePubByResult(AnalysisQueryVo.getOne(analysisQueryVo));
            //农村公共建筑安全隐患数量
            List<Map> buildingNumVillagePubByBuildingIsDamage = exportReportService.buildingNumVillagePubByBuildingIsDamage(AnalysisQueryVo.getOne(analysisQueryVo));
            //农村公共建筑安全隐患面积
            List<Map> buildingAreaVillagePubByBuildingIsDamage = exportReportService.buildingAreaVillagePubByBuildingIsDamage(AnalysisQueryVo.getOne(analysisQueryVo));
            //柱状建筑数量
            jsonObject.put("buildingNumColumn", buildingNumColumn);
            //柱状建筑面积
            jsonObject.put("buildingAreaColumn", buildingAreaColum);
            //饼状城镇建筑结构类型数量
            jsonObject.put("buildingCityNumDataByStructureType", buildingCityNumDataByStructureType);
            //饼状城镇建筑结构类型面积
            jsonObject.put("buildingCityAreaDataByStructureType", buildingCityAreaDataByStructureType);
            //饼状农村建筑结构类型数量
            jsonObject.put("buildingVillageNumDataByStructureType", buildingVillageNumDataByStructureType);
            //饼状农村建筑结构类型面积
            jsonObject.put("buildingVillageAreaDataByStructureType", buildingVillageAreaDataByStructureType);
            //饼状城镇住宅抗震设防烈度数量
            jsonObject.put("buildingNumCityByFortificationIntensity", buildingNumCityByFortificationIntensity);
            //饼状城镇住宅抗震设防烈度面积
            jsonObject.put("buildingAreaCityByFortificationIntensity", buildingAreaCityByFortificationIntensity);
            //饼状城镇非住宅抗震设防烈度数量
            jsonObject.put("buildingNumNoCityByFortificationIntensity", buildingNumNoCityByFortificationIntensity);
            //饼状城镇非住宅抗震设防烈度面积
            jsonObject.put("buildingAreaNoCityByFortificationIntensity", buildingAreaNoCityByFortificationIntensity);
            //饼状农村住宅抗震设防烈度数量
            jsonObject.put("buildingNumVillageByFortificationIntensity", buildingNumVillageByFortificationIntensity);
            //饼状农村住宅抗震设防烈度面积
            jsonObject.put("buildingAreaVillageByFortificationIntensity", buildingAreaVillageByFortificationIntensity);
            //饼状农村非住宅抗震设防烈度数量
            jsonObject.put("buildingNumVillagePubByFortificationIntensity", buildingNumVillagePubByFortificationIntensity);
            //饼状农村非住宅抗震设防烈度面积
            jsonObject.put("buildingAreaVillagePubByFortificationIntensity", buildingAreaVillagePubByFortificationIntensity);
            //饼状城镇住宅抗震设防情况数量
            jsonObject.put("buildingNumCityByResult", buildingNumCityByResult);
            //饼状城镇住宅抗震设防情况面积
            jsonObject.put("buildingAreaCityByResult", buildingAreaCityByResult);
            //饼状城镇住宅安全隐患数量
            jsonObject.put("buildingNumCityByBuildingIsDamage", buildingNumCityByBuildingIsDamage);
            //饼状城镇住宅安全隐患面积
            jsonObject.put("buildingAreaCityByBuildingIsDamage", buildingAreaCityByBuildingIsDamage);
            //饼状城镇非住宅抗震设防情况数量
            jsonObject.put("buildingNumNoCityByResult", buildingNumNoCityByResult);
            //饼状城镇非住宅抗震设防情况面积
            jsonObject.put("buildingAreaNoCityByResult", buildingAreaNoCityByResult);
            //饼状城镇非住宅安全隐患数量
            jsonObject.put("buildingNumNoCityByBuildingIsDamage", buildingNumNoCityByBuildingIsDamage);
            //饼状城镇非住宅安全隐患面积
            jsonObject.put("buildingAreaNoCityByBuildingIsDamage", buildingAreaNoCityByBuildingIsDamage);
            //饼状农村住宅抗震设防情况数量
            jsonObject.put("buildingNumVillageByResult", buildingNumVillageByResult);
            //饼状农村住宅抗震设防情况面积
            jsonObject.put("buildingAreaVillageByResult", buildingAreaVillageByResult);
            //饼状农村住宅抗震构造措施数量
            jsonObject.put("buildingNumVillageByHouseMeasures", buildingNumVillageByHouseMeasures);
            //饼状农村住宅抗震构造措施面积
            jsonObject.put("buildingAreaVillageByHouseMeasures", buildingAreaVillageByHouseMeasures);
            //饼状农村住宅曾受灾情况数量
            jsonObject.put("buildingNumVillageByOnceAffected", buildingNumVillageByOnceAffected);
            //饼状农村住宅曾受灾情况面积
            jsonObject.put("buildingAreaVillageByOnceAffected", buildingAreaVillageByOnceAffected);
            //饼状农村住宅安全隐患数量
            jsonObject.put("buildingNumVillageByBuildingIsDamage", buildingNumVillageByBuildingIsDamage);
            //饼状农村住宅安全隐患面积
            jsonObject.put("buildingAreaVillageByBuildingIsDamage", buildingAreaVillageByBuildingIsDamage);
            //饼状农村公共住宅抗震设防情况数量
            jsonObject.put("buildingNumVillagePubByResult", buildingNumVillagePubByResult);
            //饼状农村公共住宅抗震设防情况面积
            jsonObject.put("buildingAreaVillagePubByResult", buildingAreaVillagePubByResult);
            //饼状农村公共住宅安全隐患数量
            jsonObject.put("buildingNumVillagePubByBuildingIsDamage", buildingNumVillagePubByBuildingIsDamage);
            //饼状农村公共住宅安全隐患面积
            jsonObject.put("buildingAreaVillagePubByBuildingIsDamage", buildingAreaVillagePubByBuildingIsDamage);
            //饼状城镇住宅年代数量
            jsonObject.put("buildingNumCityByBuildingTime", buildingNumCityByBuildingTime);
            //饼状城镇住宅年代面积
            jsonObject.put("buildingAreaCityByBuildingTime", buildingAreaCityByBuildingTime);
            //饼状城镇非住宅年代数量
            jsonObject.put("buildingNumNoCityByBuildingTime", buildingNumNoCityByBuildingTime);
            //饼状城镇非住宅年代面积
            jsonObject.put("buildingAreaNocityeByBuildingTime", buildingAreaNocityeByBuildingTime);
            //饼状农村住宅年代数量
            jsonObject.put("buildingNumVillageByBuildingTime", buildingNumVillageByBuildingTime);
            //饼状农村住宅年代面积
            jsonObject.put("buildingAreaVillageByBuildingTime", buildingAreaVillageByBuildingTime);
            //饼状农村公共建筑年代数量
            jsonObject.put("buildingNumVillagePubByBuildingTime", buildingNumVillagePubByBuildingTime);
            //饼状农村公共建筑年代米面积
            jsonObject.put("buildingAreaVillagePubByBuildingTime", buildingAreaVillagePubByBuildingTime);
            jsonObject.put("message", "查询成功");
            restResponse = RestResponse.succeed(jsonObject);
        } catch (Exception e) {
            String errormessage = "查询失败";
            log.error(errormessage, e);
            restResponse = RestResponse.fail(errormessage);
        }
        return restResponse;
    }
}
